//
//  StatusToolbar.m
//  微博2期
//
//  Created by J on 15/12/3.
//
//

#import "StatusToolbar.h"
#import "Status.h"

@interface StatusToolbar ()
/**
 *  里面存放所有的按钮
 */
@property(nonatomic,strong)NSArray * btns;


/**
 *  里面存放所有的分割线
 */
@property(nonatomic,strong)NSArray * dividers;
@property(nonatomic,strong)UIButton * repostBtn;
@property(nonatomic,strong)UIButton * commentBtn;
@property(nonatomic,strong)UIButton * attitudeBtn;

@end
@implementation StatusToolbar


- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
  
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"timeline_card_bottom_background"]];
        self.repostBtn = [self setupBtn:@"转发" icon:@"timeline_icon_retweet"];
        self.commentBtn = [self setupBtn:@"评论" icon:@"timeline_icon_comment"];
        self.attitudeBtn = [self setupBtn:@"赞" icon:@"timeline_icon_unlike"];

    }
    return self;
}



+ (instancetype)toolbar{
    
    return  [[self alloc]init];
    
}


/**
 *  初始化一个按钮
 *
 *  @param title 按钮文字
 *  @param icon  按钮图标
 */
- (UIButton* )setupBtn:(NSString*)title icon:(NSString*)icon{
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [self addSubview:btn];
   
    
    return btn;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    
    NSUInteger count = self.subviews.count;
    CGFloat btnW = self.width/count;
    for (int i=0; i<count; i++) {
        
        UIButton * btn = self.subviews[i];
        btn.y = 0;
        btn.width = btnW;
        btn.x = i*btnW;
        btn.height = self.height;
    }
    
}

- (void)setStatus:(Status *)status{
    _status = status;
//    status.reposts_count = 587456; //58.7万
//    status.comments_count = 11004; //1万
//    status.attitudes_count = 604; //604
//    //转发
    [self setupBtnCount:status.reposts_count btn:self.repostBtn Title:@"转发"];

    //评论
    [self setupBtnCount:status.comments_count btn:self.commentBtn Title:@"评论"];

    //赞
    [self setupBtnCount:status.attitudes_count btn:self.attitudeBtn Title:@"赞"];

    
}


- (void)setupBtnCount:(NSInteger)count  btn:(UIButton*)btn Title:(NSString*)title{
    [btn setTitle:title forState:UIControlStateNormal];
    if (count) {//有数字
        
        if (count<10000) {//不足10000: 直接显示数字
            title =[NSString stringWithFormat:@"%d",count];
            
        }else{// 达到10000: 显示xxx万 不要有.0的情况
            
            double wan = count/10000.0;//整数除以整数结果==整数
            title =[NSString stringWithFormat:@"%.1f万",wan];
            //将出现的.0替换如果不出现就不会调用
          title =  [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
        }
    }
    [btn setTitle:title forState:UIControlStateNormal];
}


- (NSArray *)btns{
    
    if (_btns) {
        self.btns = self.subviews;
    }
    return _btns;
}

@end
